package cn.edu.tju.apps;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import cn.edu.tju.apps.utils.DailyData;
import cn.edu.tju.spider.UrlDataHanding;
import cn.edu.tju.spider.UrlQueue;
import cn.edu.tju.spider.VisitedUrlQueue;

@SuppressWarnings("unused")
public class FatherSpider {
	public static String srcUrl = "";
	public static String tgtUrl = "";
	public static final int TDDATA = 1;
	public static int curTask = TDDATA;

	// 一些特殊的全局变量
	public static List<DailyData> dataLst = new ArrayList<DailyData>();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		switch (curTask) {
		case TDDATA: {
			srcUrl = "http://www.sge.sh/publish/sge/xqzx/jyxq/";
			tgtUrl = "http://www.sge.sh/publish/sge/xqzx/jyxq/";
			// for (int i = 0; i < 141; i++) {
			// String targetUrl = srcUrl + "index" + (i==0? "": i) + ".htm";
			// if(!UrlQueue.isContains(targetUrl)){
			// UrlQueue.addElem(targetUrl);
			// }
			// }
			int startIndex = 11098;
			int lastIndex = 11089;
			for (int i = startIndex; i >= lastIndex; i--) {
				String targetUrl = srcUrl + i + ".htm";
				if (!UrlQueue.isContains(targetUrl)) {
					UrlQueue.addElem(targetUrl);
				}
			}
			break;
		}
		default:
			break;
		}

		UrlDataHanding[] url_Handings = new UrlDataHanding[10];
		for (int i = 0; i < 1; i++) {
			url_Handings[i] = new UrlDataHanding();
			new Thread(url_Handings[i]).start();
		}
	}

	public static void getHrefOfContent(String content) {
		System.out.println("开始");
		String[] contents = content.split("<a href=\"");
		for (int i = 1; i < contents.length; i++) {
			int endHref = contents[i].indexOf("\"");
			String aHref = contents[i].substring(0, endHref);
			if (aHref != null && !aHref.contains("../")) {
				switch (curTask) {
				case TDDATA: {
					if (aHref.endsWith(".htm")) {
						if (aHref.contains("index")) {
							break;
							// aHref = srcUrl + aHref;
						} else {
							break;
							// aHref = tgtUrl + aHref;
						}
					}
					if (!UrlQueue.isContains(aHref)
							&& (aHref.contains(srcUrl) || aHref
									.contains(tgtUrl))) {
						System.out.println(aHref);
						// UrlQueue.addElem(aHref);
					}
					break;
				}
				default:
					break;
				}
			}
		}
		System.out.println(UrlQueue.size() + "--抓取到的连接数");
		System.out.println(VisitedUrlQueue.size() + "--已处理的页面数");
	}

	public static String getContentFormUrl(String url) {
		/* 实例化一个HttpClient客户端 */
		String[] temUrl = url.split("@@@");
		if (url.contains("@@@")) {
			url = url.split("@@@")[0];
		}
		HttpClient client = new DefaultHttpClient();
		HttpGet getHttp = new HttpGet(url);

		String content = null;

		HttpResponse response;
		try {
			/* 获得信息载体 */
			response = client.execute(getHttp);
			HttpEntity entity = response.getEntity();

			VisitedUrlQueue.addElem(url);

			if (entity != null) {
				/* 转化为文本信息 */

				switch (curTask) {
				case TDDATA: {
					content = new String(EntityUtils.toByteArray(entity),
							"utf-8");
					break;
				}
				default:
					break;
				}
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			client.getConnectionManager().shutdown();
		}
		if (content == null) {
			if (!UrlQueue.isContains(url)) {
				UrlQueue.addElem(url);
			}
			return "";
		}
		loadData(url, content);

		return content;
	}

	private static void loadData(String url2, String content) {
		// TODO Auto-generated method stub
		if (url2.contains(tgtUrl)) {
			switch (curTask) {
			case TDDATA: {
				if (content.contains("<tbody>")) {
					System.out
							.println("processing --------------------------------"
									+ url2);
					DailyData newDayItem = new DailyData();
					Map<String, Double> dataMap = null;
					String dateStr = content
							.split("<div class=\"fullxiangguan_zuozhe\">")[1]
							.split("<p >")[1].split(" &nbsp;&nbsp;&nbsp;")[0];
					dateStr = dateStr.replaceAll("年", "-").replaceAll("月", "-")
							.replaceAll("日", "");
					System.out.println(dateStr);
					newDayItem.dateStr = dateStr;
					content = content.split("<tbody>")[1].split("</tbody>")[0];
					// System.out.println("before data processing ---- "+content);
					String[] tem1Str = content.split("<script");
					String cntTemStr = "";
					for (int i = 0; i < tem1Str.length; i++) {
						if (tem1Str[i].contains("</script>")) {
							if (tem1Str[i].split("</script>").length > 1) {
								cntTemStr += tem1Str[i].split("</script>")[1];
							}
						} else {
							cntTemStr += tem1Str[i];
						}
					}
					content = cntTemStr;
					String dataStr = "";
					String dataType = "";
					for (int i = 2; i < content.split("<tr").length; i++) {
						dataMap = new HashMap<String, Double>();
						dataStr = content.split("<tr")[i].split("</tr>")[0];
						String[] tem2Str = { "", "", "", "", "", "", "", "",
								"", "", "", "" };
						int strIndex = 0;

						for (int j = 0; j < dataStr.split(">").length; j++) {
							if (strIndex > 11) {
								break;
							}
							if (dataStr.split(">")[j].contains("<")) {
								String tarStr = dataStr.split(">")[j]
										.split("<")[0];
								if (tarStr.length() > 0
										&& !tarStr.contains("\n")
										&& !tarStr.isEmpty()) {
									tem2Str[strIndex] = tarStr
											.replaceAll("%", "")
											.replaceAll(",", "")
											.replaceAll("&nbsp;", "")
											.replaceAll(" ", "")
											.replaceAll("　", "");
									if (tem2Str[strIndex].isEmpty()) {
										continue;
									}
									if (tem2Str[strIndex].startsWith(".")
											&& !tem2Str[strIndex - 1]
													.contains(".")) {
										tem2Str[strIndex - 1] += tem2Str[strIndex];
										--strIndex;
									} else if (strIndex > 0
											&& !tem2Str[strIndex].contains(".")
											&& tem2Str[strIndex - 1]
													.endsWith(".")) {
										tem2Str[strIndex - 1] += tem2Str[strIndex];
										--strIndex;
									}
									System.out.println("----"
											+ tem2Str[strIndex] + "----");
									strIndex++;
								}
							}
						}
						for (int j = 0; j < DailyData.strSet.length; j++) {
							if (tem2Str[j + 1].isEmpty()
									|| "".equals(tem2Str[j + 1])) {
								break;
							}
							dataMap.put(DailyData.strSet[j],
									Double.valueOf(tem2Str[j + 1]));
						}
						dataType = tem2Str[0];
						System.out.println(dataType);
						newDayItem.setData(dataType, dataMap);
					}
					if (!dataLst.contains(newDayItem)) {
						dataLst.add(newDayItem);
					}
				}
				break;
			}
			default:
				break;
			}
		}
	}
}
